package com.easylinkin.linkappapi.lobar.controller;

import com.easylinkin.linkappapi.lobar.dto.LaborRegionDTO;
import com.easylinkin.linkappapi.lobar.dto.region.UserRegionDTO;
import com.easylinkin.linkappapi.lobar.dto.screen.LobarScreenDTO;
import com.easylinkin.linkappapi.lobar.entity.LaborRegion;
import com.easylinkin.linkappapi.lobar.service.LaborRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 区域坐标管理
 */
@RestController
@RequestMapping("laborRegion")
@Api(tags = "区域坐标管理")
public class LaborRegionController {
  
  @Autowired
  private LaborRegionService laborRegionService;

  /**
   * @Description: 增加区域坐标
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping
  @ApiOperation("增加区域坐标")
  public RestMessage insert(@RequestBody LaborRegionDTO laborRegionDTO){
    //参数验证
    laborRegionService.insert(laborRegionDTO);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 删除区域坐标（包含批量删除）
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping
  @ApiOperation("删除区域坐标（包含批量删除）")
  public RestMessage delBatch(@RequestBody LaborRegion laborRegion){
    Assert.notEmpty(laborRegion.getIds(),"id不能为空");
    laborRegionService.removeByIds(laborRegion.getIds());
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 编辑区域坐标
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping
  @ApiOperation("编辑区域坐标")
  public RestMessage updateById(@RequestBody LaborRegionDTO laborRegionDTO){
    Assert.notNull(laborRegionDTO.getId(),"id不能为空");
    laborRegionService.updateOne(laborRegionDTO);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据id查询区域坐标详情
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("根据id查询区域坐标详情")
  public RestMessage findById(@PathVariable("id")Long id) {
    Assert.notNull(id,"id不能为空");
    LaborRegionDTO laborRegionDTO = laborRegionService.findById(id);
    return RestBuilders.successBuilder().data(laborRegionDTO).build();
  }

  /**
   * @Description: 根据条件，分页(不分页)查询
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("list")
  @ApiOperation("根据条件，分页(不分页)查询")
  public RestMessage queryList(){
    List<LaborRegionDTO> record =  laborRegionService.queryList();
    return RestBuilders.successBuilder().data(record).build();
  }

  /**
   * 人员坐标上传
   */
  @PostMapping("region")
  @ApiOperation("人员坐标上传")
  public RestMessage insertRegion(@RequestBody UserRegionDTO userRegionDTO) throws IOException {
    //参数验证
    laborRegionService.insertRegion(userRegionDTO);
    return RestBuilders.successBuilder().build();
  }

  /**
   * 人员坐标获取
   */
  @GetMapping("region")
  @ApiOperation("人员坐标获取")
  public RestMessage getRegions() throws IOException {
    //参数验证
    List<UserRegionDTO> userRegionDTOS = laborRegionService.getRegions();
    return RestBuilders.successBuilder().data(userRegionDTOS).build();
  }

//  室外人员定位代码都放这里
  /**
   * 在场人数(施工区,办公区,生活区)
   * @param userWorkType 人员类型 1:作业人员 2:管理人员
   */
  @GetMapping("onNum")
  public RestMessage onNum(Integer userWorkType) throws IOException {
    List<LobarScreenDTO> lobarScreenDTOS = laborRegionService.onNum(userWorkType);
    return RestBuilders.successBuilder().data(lobarScreenDTOS).build();
  }

  /**
   * 人员分类统计
   */
  @GetMapping("groupNum")
  public RestMessage groupNum(Integer userWorkType) throws IOException {
    Map<String,List<LobarScreenDTO>> map = laborRegionService.groupNum(userWorkType);
    return RestBuilders.successBuilder().data(map).build();
  }
}

